package cn.zcj.studytest.service;

import cn.zcj.studytest.ThrowData.Result;
import cn.zcj.studytest.domain.Comment;
import cn.zcj.studytest.domain.Image;
import cn.zcj.studytest.domain.SignUp;
import cn.zcj.studytest.dto.CommentDetailDTO;
import cn.zcj.studytest.dto.CommentInsertDTO;
import cn.zcj.studytest.dto.CommentQueryDTO;
import cn.zcj.studytest.mapper.CommentMapper;
import cn.zcj.studytest.mapper.InsertContentMapper;
import cn.zcj.studytest.vo.CommentVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CommentService extends ServiceImpl<CommentMapper, Comment> {

    @Resource
    private InsertContentService insertContentService;
    @Resource
    private SignUpService signUpService;

    public Result<Object> insertComment(CommentInsertDTO commentInsertDTO) {
        Result<Object> result = new Result<>();
        Comment comment = new Comment();
        comment.setMessage(commentInsertDTO.getMessage());
        comment.setCreateUserId(commentInsertDTO.getCreateUserId());
        comment.setColor(commentInsertDTO.getColor());
        comment.setProductId(commentInsertDTO.getProductId());
        this.save(comment);
        return result.ok("请求成功");
    }

    public Result<List<CommentVO>> queryComment(CommentQueryDTO commentQueryDTO) {
        Result<List<CommentVO>> result = new Result<>();
        List<Image> imageList = insertContentService.queryByUserId(commentQueryDTO.getCreateUserId());
        Map<Long, byte[]> imageMap = imageList.stream().collect(Collectors.toMap(Image::getId, Image::getImg, (o, n) -> n));
        List<Comment> commentList = this.lambdaQuery().in(Comment::getProductId, imageMap.keySet()).list();
        Map<Long, List<Comment>> commentMap = commentList.stream().collect(Collectors.groupingBy(Comment::getProductId));

        SignUp signUp = signUpService.getById(commentQueryDTO.getCreateUserId());
        List<CommentVO> commentVOS = imageList.stream().map(e -> {
            CommentVO commentVO = new CommentVO();
            commentVO.setImage(imageMap.get(e.getId()));
            commentVO.setDetailList(commentMap.getOrDefault(e.getId(), Collections.emptyList()).stream().map(f -> {
                CommentDetailDTO commentDetailDTO = new CommentDetailDTO();
                commentDetailDTO.setCreateUserName(signUp.getUsername());
                commentDetailDTO.setCreateTime(f.getCreateTime());
                commentDetailDTO.setMessage(f.getMessage());
                commentDetailDTO.setColor(f.getColor());
                return commentDetailDTO;
            }).sorted(Comparator.comparing(CommentDetailDTO::getCreateTime).reversed()).collect(Collectors.toList()));
            return commentVO;
        }).toList();
        return result.ok(commentVOS);
    }

    public Result<CommentVO> queryCommentSingle(CommentQueryDTO commentQueryDTO) {
        Result<CommentVO> result = new Result<>();
        Long productId = commentQueryDTO.getProductId();
        List<Comment> commentList = this.lambdaQuery().eq(Comment::getProductId, productId).list();
        CommentVO commentVO = new CommentVO();
        commentVO.setDetailList(commentList.stream().map(e ->{
            CommentDetailDTO commentDetailDTO = new CommentDetailDTO();
            commentDetailDTO.setCreateUserName(signUpService.getById(e.getCreateUserId()).getUsername());
            commentDetailDTO.setCreateTime(e.getCreateTime());
            commentDetailDTO.setMessage(e.getMessage());
            commentDetailDTO.setColor(e.getColor());
            return commentDetailDTO;
        }).sorted(Comparator.comparing(CommentDetailDTO::getCreateTime).reversed()).collect(Collectors.toList()));
        commentVO.setImage(insertContentService.getById(productId).getImg());
        return result.ok(commentVO);
    }
}
